package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：登录控制器
 *
 * @Package: com.super520.rbac.web.controller
 * 类名:@LoginController.java
 * @author: PengQi
 * @date: 2019年4月6日 下午10:17:00
 */
@Controller
public class LoginController {
    // 注入业务层组件
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public Object login(User user) {
        JsonResult jsonResult = new JsonResult();
        try {
            // 调用登录校验业务
            userService.login(user,null);
        } catch (Exception e) {
            // 登录失败后的业务操作
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
            return jsonResult;
        }
        // 登录成功
        return jsonResult;
    }

    @RequestMapping("/logout")
    public String logout() {
        // 注销登录
        UserContext.removeUserInfo();
        return "redirect:/";
    }
}
